package com.ebt.m.policy.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.FilterDataBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Damon.Han
 */
public class FilterPresenter extends BaseListPresenter<BaseListContract.IView> {
    public static final int DATA_VIEW_TITLE = 1;
    public static final int DATA_VIEW_TYPE = 2;

    public FilterPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getFilterCollections()
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(
                        response -> {
                            iView.updateData(createSectionItem(response.getData()));
                        }, throwable -> {
                            throwable.printStackTrace();
                            iView.errorData();
                        });
    }

    @Override
    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null) {
            return sections;
        }
        FilterDataBean filterDataBean = null;
        for (int i = 0; i < list.size(); i++) {
            filterDataBean = (FilterDataBean) list.get(i);
            sections.add(new SectionItem(DATA_VIEW_TITLE, filterDataBean));
            if (filterDataBean.getItem() != null) {
                for (int j = 0; j < filterDataBean.getItem().size(); j++) {
                    filterDataBean.getItem().get(j).setKey(filterDataBean.getParamKey());
                }
                sections.add(new SectionItem(DATA_VIEW_TYPE, filterDataBean.getItem()));
            }
        }
        return sections;
    }

    @Override
    public void refresh() {

    }

    @Override
    public void loadNext(Object... args) {
    }

}
